% $Id: OBJFloorLoader.m 7 2009-01-27 12:46:40Z Jie.Bao $

classdef OBJFloorLoader
%OBJLOADER Summary of this class goes here
%   Detailed explanation goes here

   properties
       Vertices;
       Faces;
       File=''; 
       %ScalingFactor;
   end

   methods
       function this = OBJFloorLoader(filename)
           this.File = filename;
           %this.ScalingFactor = factor;
           input = fopen(this.File);
           
           v = 1;
           f = 1;
           
           line = fgetl(input);
           while ischar(line)
               if isempty(line)
                   line = fgetl(input);
                   continue
               end
               ch = line(1);
               
               
               if (ch == '#')
                   line = fgetl(input);
                   continue
               elseif ch == 'v' && line(2) == ' '
                   pts = sscanf(line,'v %f %f %f');
                   
                   this.Vertices(v,1) = pts(1); 
                   this.Vertices(v,2) = pts(2);
                   this.Vertices(v,3) = pts(3);
                       
                   v = v+1;
               elseif ch == 'f'
                   pts = sscanf(line,'f %f %f %f');
                   
                   this.Faces(f,1) = pts(1); 
                   this.Faces(f,2) = pts(2);
                   this.Faces(f,3) = pts(3);
                   
                   f= f+1;
               end
               line = fgetl(input);
           end
           fclose(input)
       end
       
       function Plot(this, scale, x, y)
           for j=1:length(this.Faces)
               line( [this.Vertices(this.Faces(j,1),1)/scale + x,  ...
                      this.Vertices(this.Faces(j,2),1)/scale + x,  ...
                      this.Vertices(this.Faces(j,3),1)/scale + x], ...
                     [this.Vertices(this.Faces(j,1),2)/scale + y,  ...
                      this.Vertices(this.Faces(j,2),2)/scale + y,  ...
                      this.Vertices(this.Faces(j,3),2)/scale + y], ...
                      'Color', 'k');
           end
       end
   end
end 
